/*
 * @Author: EltonLi lijiaming@uavi-tech.com
 * @Date: 2023-08-30 17:06:18
 * @LastEditors: LVGRAPE
 * @LastEditTime: 2025-05-19 19:36:19
 * @FilePath: \zino-fc-v4\applications\main.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE[]
 */
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-08-30     RT-Thread    first version
 */

#include <rtthread.h>
#include "program.h"
#include "fc_error.h"
//  #include "ledbar.h"

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include <rtdevice.h>

#ifdef ARDUINO
#include "arduino.h"
#endif

extern void setup(void);
extern void loop(void);

extern void RT_triger_handle(void);
extern void LT_triger_handle(void);
int main(void)
{
#ifdef ARDUINO
    pro_event = rt_event_create("pro_event", RT_IPC_FLAG_FIFO);
    rt_thread_delay(5);
    setup();
    // ledbar_i2cbus_init();
#endif
    delay(100);
    while (1)
    {
        if (get_fc_main_state() == true)
        {
            loop();
        }
        else
        {
            uint32_t event = 0;
            // rt_kprintf("program loop ...\n");
            if(FC_ERROR_CHECK(FC_PROGRAM_MODE) && FC_ERROR_CHECK(FC_PRORAM_READY))
            {
                // rt_kprintf("program waiting ...\n");
                if (rt_event_recv(pro_event, PRO_EVNET_RT_TRIGGER | PRO_EVNET_LT_TRIGGER | PRO_EVENT_DISABLE, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &event) == RT_EOK)
                {
                    // loop();
                    if(event & PRO_EVNET_RT_TRIGGER)
                    {
                        // rt_kprintf("RT trigger\n");
                        RT_triger_handle();
                    }
                    if(event & PRO_EVNET_LT_TRIGGER)
                    {
                        // rt_kprintf("LT trigger\n");
                        LT_triger_handle();
                    }
                    if(event & PRO_EVENT_DISABLE)
                    {
                        // rt_kprintf("program disable\n");
                    }
                }
            }
        }
        // ledbar_rgbbuffer_set_one(0, 80, 5, 0, 0);
        // rt_thread_mdelay(1000);
        // ledbar_rgbbuffer_set_one(0, 80, 0, 5, 0);
        // rt_thread_mdelay(1000);
        // ledbar_rgbbuffer_set_one(0, 80, 0, 0, 5);
        // rt_thread_mdelay(1000);
        // ledbar_rgbbuffer_set_one(0, 80, 5, 5, 5);
        // rt_thread_mdelay(1000);
        rt_thread_mdelay(1);
    }

    return RT_EOK;
}
